﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using WMPLib;

namespace 音乐播放器
{
    public partial class Form1 : Form
    {
        //声明一个list，用来存储文件的路径
        private List<string> urlList = new List<string>(); //泛型

        private double max, min;

        public Form1()
        {
            InitializeComponent();
        }

        //添加歌曲
        private void btnInput_Click(object sender, EventArgs e)
        {
            // 实例化一个打开文件的对话框
            OpenFileDialog of = new OpenFileDialog();
            //让选择器可以同时选择多个文件
            of.Multiselect = true;
            of.Title = "请选择音乐文件";
            //指定选择文件的类型
            of.Filter = "音乐|*.mp3; *.wma";//能放入更多的类型的音乐
            // of.Filter = "(*.mp3)|*.mp3";//只能放.mp3的

            //确认用户选择的是确定按钮
            if (of.ShowDialog() == DialogResult.OK)
            {
                //把用户新选择的文件存储到数组中
                string[] nameList = of.FileNames;
                // string[] nameList = of.SafeFileNames;

                //读取数组中的数据
                foreach (string url in nameList)
                {
                    //Path.GetFileNameWithoutExtension(url)获取不具有扩展名的文件名
                    listBoxMusics.Items.Add(Path.GetFileNameWithoutExtension(url));
                    urlList.Add(url);
                }
            }
        }

        //播放
        private void btnPlay_Click(object sender, EventArgs e)
        {
            //ListBox中的索引和urlList中的索引相对应
            //获取当前选中歌曲多的索引
            int selectedIndex = listBoxMusics.SelectedIndex;
            if (selectedIndex < 0)
            {
                //判断列表中是否有选中的歌曲，有的话，播放选中的，没有的话，播放第一首
                selectedIndex = selectedIndex < 0 ? 0 : selectedIndex; //三元运算符
                                                                       //更新选中行 重新设置当前选中行的索引
                listBoxMusics.SelectedIndex = selectedIndex;
                //把urlList中存储的url地址赋给播放器控件
                axWindowsMediaPlayer1.URL = urlList[selectedIndex];
                lblMusicName.Text = listBoxMusics.SelectedItem.ToString();
            }
            else
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }
            timer1.Enabled = true;
        }

        //暂停
        private void btnPause_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.pause();
        }

        //停止
        private void btnStop_Click(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            timer1.Enabled = false;
        }

        //上一首
        private void btnLast_Click(object sender, EventArgs e)
        {
            //ListBox中的索引和urlList中的索引相对应
            //获取当前选中歌曲多的索引
            int selectedIndex = listBoxMusics.SelectedIndex - 1;
            selectedIndex = selectedIndex < 0 ? 0 : selectedIndex; //三元运算符
            //更新选中行 重新设置当前选中行的索引
            listBoxMusics.SelectedIndex = selectedIndex;
            //把urlList中存储的url地址赋给播放器控件
            axWindowsMediaPlayer1.URL = urlList[selectedIndex];
            lblMusicName.Text = listBoxMusics.SelectedItem.ToString();
        }

        //下一首
        private void btnNext_Click(object sender, EventArgs e)
        {
            //ListBox中的索引和urlList中的索引相对应
            //获取当前选中歌曲多的索引
            int selectedIndex = listBoxMusics.SelectedIndex + 1;
            selectedIndex = selectedIndex == listBoxMusics.Items.Count ? listBoxMusics.SelectedIndex : selectedIndex;
            //三元运算符
            //感觉上面的判断应该写成这样
            //    selectedIndex = selectedIndex >=listBoxMusics.Items.Count  ? listBoxMusics.selectedIndex : selectedIndex;
            //更新选中行 重新设置当前选中行的索引
            listBoxMusics.SelectedIndex = selectedIndex;
            //把urlList中存储的url地址赋给播放器控件
            axWindowsMediaPlayer1.URL = urlList[selectedIndex];
            lblMusicName.Text = listBoxMusics.SelectedItem.ToString();
        }

        //列表选择发生变化时，播放选中的歌曲
        private void listBoxMusics_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ListBox中的索引和urlList中的索引相对应
            //获取当前选中歌曲多的索引
            int selectedIndex = listBoxMusics.SelectedIndex;
            //把urlList中存储的url地址赋给播放器控件
            axWindowsMediaPlayer1.URL = urlList[selectedIndex];
            timer1.Enabled = true;
        }

        //鼠标按下
        private void trackBar1_MouseDown(object sender, MouseEventArgs e)
        {
            //暂停播放
            timer1.Enabled = false;
            axWindowsMediaPlayer1.Ctlcontrols.pause();
        }

        //鼠标抬起
        private void trackBar1_MouseUp(object sender, MouseEventArgs e)
        {
            double doValue = trackBar1.Value; //获取被拖动以后的位置
            axWindowsMediaPlayer1.Ctlcontrols.currentPosition = doValue;//重置播放位置
            axWindowsMediaPlayer1.Ctlcontrols.play();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //获取文件的长度 时间长度
            max = axWindowsMediaPlayer1.currentMedia.duration;
            //获取当前歌曲的播放位置
            min = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
            //类型强转
            //float len = 18F;
            //int len1 = (int)len;
            //Convert.ToInt32("");
            //int.Parse("");
            trackBar1.Maximum = (int)max;
            trackBar1.Value = (int)min;
            //一首歌播放完成后，继续下一首
            if (axWindowsMediaPlayer1.playState == WMPPlayState.wmppsStopped)
            {
                //当前歌曲播放结束后，要获取下一首歌曲的索引值
                int selectedIndex = listBoxMusics.SelectedIndex + 1;
                selectedIndex = selectedIndex == listBoxMusics.Items.Count ? 0 : selectedIndex;
                //目的循环播放
                axWindowsMediaPlayer1.URL = urlList[selectedIndex];
                listBoxMusics.SelectedIndex = selectedIndex;
                lblMusicName.Text = listBoxMusics.SelectedIndex.ToString();
                trackBar1.Value = 0;
                timer1.Enabled = true;
            }
        }
    }
}